import requests
from bs4 import BeautifulSoup as bs
import time

url = 'https://www.umei.cc/bizhitupian/weimeibizhi/'
resp = requests.get(url=url)
resp.encoding = 'utf-8'
# print(resp.text)

# 指定 html 解析器
page = bs(resp.text, 'html.parser')

# 从 BeautifulSoup 中查数据
# find(标签名，属性=值): 找第一个
# find_all(标签名，属性=值): 找全部

pictures = page.find_all('div', attrs={'class':'item masonry_brick'})
# print(pictures)

# 拿到所有数据
for picture in pictures:
    a = picture.find('a')
    href = a.get('href')
    # print(href)
    detailUrl = 'https://www.umei.cc' + href
    # print(detailUrl)
    resp2 = requests.get(url=url)
    resp2.encoding = 'utf-8'
    # print(resp2.text)
    page = bs(resp2.text, 'html.parser')

    div = page.find('div', attrs={'class':'big-pic'})
    print(div)
    img = div.find('img')
    print(img.get('src'))
    src = img.get('src')

    # 下载图片
    img_resp = requests.get(src)
    # 拿到字节数据
    img_resp.content
    img_name = src.split("/")[-1]
    with open(img_name, mode="wb") as f:
        f.write(img_resp.content)
    f.close()
    # 睡眠一段时间，防止 ip 被禁
    time.sleep(1)

